<?php
namespace mobadmin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\session\Session;
use herosphp\utils\AjaxResult;
use api\utils\SaasApiClient;
use herosphp\core\Log;

/**
 * @author yu
 */
class BrandAction extends CommonAction
{

    /**
     * 自定义牌号库->列表
     * @param HttpRequest $request
     */
    public function index(HttpRequest $request)
    {


        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 12;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = '';

        if ($this->getLoginUserId()) {

            $search_arr['saas_user_id'] = $this->getLoginUserId();
        }

        $service = Beans::get('brand.brand.service');

        $result = $service->getItems($pagesize, $page, array('addtime' => '-1'), $search_arr);
        $this->generatePageMenu($result['total']);

        $this->assign('items', $result['items']);
        $this->assign("title", "牌号列表");
        $this->setView("brand/brand_index");


    }

    /**
     * 自定义牌号库->添加
     * @param HttpRequest $request
     */
    public function add(HttpRequest $request)
    {
        $dataId = $request->getParameter('dataId');
        $offerFrom = $request->getParameter('offerFrom');
        $offerFromId = $request->getParameter('offerFromId');


        $this->assign("title", "自定义牌号库");

        $this->assign("dataId", $dataId);
        $this->assign("offerFrom", $offerFrom);

        $this->assign("offerFromId", $offerFromId);

        $this->setView("brand/brand_add");

    }

    /**
     * 添加牌号
     * @param HttpRequest $request
     */
    public function do_add(HttpRequest $request)
    {

        $data = $request->getParameter('data');
        $dataId = $request->getParameter('dataId');
        $offerFrom = $request->getParameter('offerFrom');
        $offerFromId = $request->getParameter('offerFromId');



        $arr['name'] = $data['name'];
        $arr['typeid'] = "{$data['typeid']}";//类型
        $arr['brandid'] = "{$data['brandid']}";//品名
        $arr['facid'] = "{$data['facid']}";//厂家
        $arr['saas_user_id'] = "{$this->getLoginUserId()}";
        $arr['from'] = "2";//标识自身应用


        $service = Beans::get('brand.brand.service');

        $result = $service->add($arr);

        //判断是否有dataId参数，有说明是在添加报价时，选择牌号没有，才跳转到添加牌号页面的，此时添加完新牌号后，回跳到添加报价页面

        if ($dataId != '') {
            $resultData = $result->getData();
            $gradeid = $resultData['id'];

            $tpfb = "{$arr['typeid']}-{$arr['brandid']}-{$arr['facid']}-{$gradeid}";
            if($offerFrom=='add'){

                $jumpUrl = "/mobadmin/offer/add?data_id={$dataId}&type_name={$data['type_name']}&product_name={$data['product_name']}&factory_name={$data['factory_name']}&brand_name={$arr['name']}&tpfb={$tpfb}";

            }else{

                $jumpUrl = "/mobadmin/offer/detail?offer_id={$offerFromId}&data_id={$dataId}&type_name={$data['type_name']}&product_name={$data['product_name']}&factory_name={$data['factory_name']}&brand_name={$arr['name']}&tpfb={$tpfb}";

            }


            AjaxResult::ajaxResult('002', $result->getMessage(), $jumpUrl);

        }

        if ($result) {

            AjaxResult::ajaxResult('000', $result->getMessage());
        } else {

            AjaxResult::ajaxResult('001', 'error');

        }

    }


    /**
     * 牌号搜索
     * @param $brand_name
     */
    public function searchBrand(HttpRequest $request)
    {

        $brand_name = $request->getParameter('brand_name');
        $brand_name2 = $request->getParameter('brand_name2');
        $saas_user_id = $this->getLoginUserId();

        $url = "api/brand/searchBrand";
        $result = SaasApiClient::send($url, array('brand_name' => "$brand_name", 'brand_name2' => "$brand_name2",'saas_user_id'=>"$saas_user_id"));
        $letterArr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

        if ($result->isSucess()) {

            $resultData = array();

            $items = $result->getData();

            foreach ($letterArr As $k1 => $letter) {

                $lt_items = array();

                foreach ($items['public'] As $k2 => $item) {

                    if (matchFirstLetter($item['brand'], $letter)) {

                        $lt_items[] = $item;
                    }
                }

                if ($lt_items) {

                    $resultData['public'][] = array('lt' => $letter, 'lt_items' => $lt_items);
                }
            }
            $resultData['custom']=$items['custom'];
            echo json_encode($resultData);

        } else {
            Log::info($result->getMessage());
            echo json_encode(array());
        }
        die;

    }

}
